<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- 通用的工具栏组件 -->
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>

        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import?type=user')">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog = true">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 表格组件 -->
      <el-card v-loading="loading" style="margin-top: 10px;">
        <el-table :data="list" border>
          <el-table-column label="序号" sortable="" type="index" :index="indexMethod" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="手机号" sortable="" prop="mobile" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="formatEmployment" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable>
            <template #default="{ row:{timeOfEntry} }">
              {{ timeOfEntry | formatTime('YYYY-MM-DD') }}
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row:{id}}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="del(id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :current-page="page"
            :page-sizes="[1,2,3,4,5,6,7,8,9,10]"
            :page-size="size"
            layout="total,sizes,prev, pager, next,jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
      <!-- 弹层组件 -->
      <AddEmployee :show-dialog.sync="showDialog" />

    </div>
  </div>
</template>

<script>
import { reqGetUserList, reqDelEmployee } from '@/api/employees'
import employeeConstant from '@/constant/employees'
import AddEmployee from '@/views/employees/components/AddEmployee.vue'
export default {
  name: 'Employees',
  components: {
    AddEmployee
  },
  data() {
    return {
      list: [],
      page: 1,
      size: 10,
      total: 0,
      loading: false,
      showDialog: false

    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      this.loading = true
      const { data: { rows, total }} = await reqGetUserList(this.page, this.size)
      this.list = rows
      this.total = total
      this.loading = false
    },
    handleSizeChange(value) {
      this.size = value
      this.page = 1
      this.getUserList()
    },
    handleCurrentChange(value) {
      this.page = value
      this.getUserList()
    },
    indexMethod(index) {
      return index + 1 + this.size * (this.page - 1)
    },
    del(id) {
      this.$confirm('确认要删除该员工吗', '温馨提示').then(async() => {
        const res = await reqDelEmployee(id)
        console.log(res)
        this.$message.success('删除员工成功')
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.getUserList()
      }).catch(() => {
        console.log('取消')
      })
    },
    formatEmployment(row, column, cellValue, index) {
      // row  行数据
      // column 列数据
      // index 下标
      // cellValue  1-正式工 -2-临时工  3-钟点工
      const { hireType } = employeeConstant
      const res = hireType.find(item => item.id === +cellValue)
      return res ? res.value : '未知'
    }
  }
}
</script>

<style>

</style>
